<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1">
	<meta name="description" content="A fully featured admin theme which can be used to build CRM, CMS, etc.">
	<meta name="author" content="Coderthemes">
	
	<!-- jQuery  -->
    <script type="text/javascript" src="/static/plugins/jquery/jquery-3.6.0.min.js"></script>
	
	<!-- layui -->
	<link rel="stylesheet" href="/static/plugins/layui/2.5.6/css/layui.css">
	<script src="/static/plugins/layui/2.5.6/layui.js" charset="utf-8"></script>
	
	<!-- layui dtree -->
	<link rel="stylesheet" href="/static/plugins/layui/extends/dtree/dtree.css">
	<link rel="stylesheet" href="/static/plugins/layui/extends/dtree/font/dtreefont.css">
    
    <!-- jquery icheck-->
    <link href="/static/plugins/icheck/skins/all.css" rel="stylesheet" type="text/css">
    <link href="/static/plugins/icheck/skins/square/blue.css" rel="stylesheet" type="text/css">
	<script src="/static/plugins/icheck/js/icheck.min.js"></script>
	
	<!-- 基于laiui的表格 -->
	<script type="text/javascript" src="/static/plugins/layui/extends/sku/simple_table_role_auth.js"></script>
	
	<style>
	.margin_l10{margin-left: 10px;}
	</style>
</head>
<body>
<div class="layui-fluid">
	<div class="layui-row">
	
		<div class="layui-col-md2">
			<div class="left-tree-div" style="height:800px; overflow: scroll;">
				<div id="id_tree"></div>
			</div>
		</div>
		<div class="layui-col-md10">
			
			<div id="simple_table_role_auth" style="padding: 30px 10px 0 20px;">
			</div>
		</div>
	</div>
</div>
<script>
	$(function () {
		layui.extend({
			dtree: '{/}/static/plugins/layui/extends/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
		}).use(['dtree','layer','jquery'], function(){
			var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
			
			// 初始化树
			window.DemoTree = dtree.render({
				elem: "#id_tree",
				url: '/api/inter/ysRole/dtreeForAuth?roleId=' + '[[${dto.roleId}]]',
				checkbar: false,
				checkbarType: 'self',
				initLevel: '2',
				width:'100%',
				menubar:true,
				menubarTips:{
					group:["refresh"]
				},
				checkbarFun:{
					chooseBefore: function($i, node) {
						if (node.level == 1 || node.level == 2) {
							
							$.ajax({
								async: false,
								type: "post",
								url: "/api/inter/ysRole/authMenu",
								data: {
									roleId : '[[${dto.roleId}]]',
									guidTsMenuInfo : node.nodeId,
									isSelected : node.checked == '0' ? 1 : 0
								},
								dataType: "json",
								success: function (data) {
									if (data.code == 0) {
										return true;
									} else {
										return false;
									}
								},
								error: function () {
									alert("ajax错误！")
								}
							});
						} else if (node.level == 3) {
							
							$.ajax({
								async: false,
								type: "post",
								url: "/api/inter/ysRole/authModule",
								data: {
									roleId : '[[${dto.roleId}]]',
									guidTsDictionaryForModule : node.nodeId,
									isSelected : node.checked == '0' ? 1 : 0
								},
								dataType: "json",
								success: function (data) {
									if (data.code == 0) {
										return true;
									} else {
										return false;
									}
								},
								error: function () {
									alert("ajax错误！")
								}
							});
						} else if (node.level == 4) {
							$.ajax({
								async: false,
								type: "post",
								url: "/api/inter/ysRole/authButton",
								data: {
									roleId : '[[${dto.roleId}]]',
									guidYsButton : node.nodeId,
									isSelected : node.checked == '0' ? 1 : 0
								},
								dataType: "json",
								success: function (data) {
									if (data.code == 0) {
										return true;
									} else {
										return false;
									}
								},
								error: function () {
									alert("ajax错误！")
								}
							});
						}
						return true;
					},
					chooseDone: function(checkbarNodesParam) {
						console.log(checkbarNodesParam);
						return false;
					}
				}
			});
		});
		
		
		var tableOfSysDict = null;
		//使用layui的sku模块
		layui.use(['form', 'simple_table_role_auth', 'jquery'], function(){
			var $ = layui.jquery,simple_table_role_auth = layui.simple_table_role_auth,form = layui.form;

			var tableOfRoleAuth = null;
			
			// 加载所有按钮
			var menuId = null;
			loadRoleAuth(menuId);
		});
		
		function loadRoleAuth(menuId) {
			$.ajax({
				type: "post",
				url: "/api/inter/ysRole/listForAuth",
				data: {
					roleId: [[${dto.roleId}]],
					menuId: menuId
				},
				dataType: "json",
				success: function (data) {
					var menuArr = data.data.menuArr;
					tableOfRoleAuth = layui.simple_table_role_auth.init({ id:'simple_table_role_auth', data: menuArr, roleId:[[${dto.roleId}]]});
				},
				error: function () {
				    alert("加载失败！")
				}
			});
		}
	});
</script>
</body>
</html>